`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    13:46:31 05/29/2013 
// Design Name: 
// Module Name:    OnBoardSwitchesController 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Revision 0.02 - Andrew - parameterized the number of switches. maintaining backwards
//				compatibility by using a default of 8.
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module OnBoardSwitchesController #(parameter numSwitches = 8)(
	 input  clk,
	 input reset,
	 input enable,
	 input [3:0] address,
    input [31:0] dataIn,
	 output [31:0] dataOut,
	 input write,
	 output done,
	 output reg interrupt,
	 input [numSwitches-1:0] switchPins
    );
	 parameter DRP_ID = 32'hA001002A;
	 
	 //reg [numSwitches-1:0] slowSwitchPins;
	 
	 assign dataOut = address == 4'hF ? DRP_ID : switchPins;
	 assign done = 1;
	 
	 reg [numSwitches-1:0] lastRead;
	 reg [numSwitches-1:0] switchPinsReg;
	 
	 reg [15:0] c;
	 always @ ( posedge clk  )
	 begin
	 
		switchPinsReg <= switchPins;
		
		if( ( ( c > 50000) & enable & ~write) | reset )
		begin
			c <= 0;
			interrupt <= 0;
			lastRead <= switchPinsReg; 
		end
		else
		begin
			interrupt <= lastRead != switchPinsReg;
		end
		c <= c + 1;
	 end
endmodule
